local skynet = require "skynet" 
require "skynet.manager"	-- import skynet.register
local json   = require "cjson"  
local DATA = require "module/data" 
json.encode_sparse_array(true)
  
CMD = {}
  
local club_info = {}    
local game_club_user = {}
local club_user_game = {}
local club_user_score = {}
function CMD.get_club_info(club_id)
	club_id = math.floor(club_id)
	if club_info[club_id] == nil then  
		club_info[club_id] = {}
        local rs = skynet.call("MG_SAVE", "lua", "get_club",0,10,{club_id=club_id})
        if rs ~= nil then  
	        for i,v in pairs(rs) do  
	        	club_info[v.club_id] = v
	        end
        else
        	return nil
        end
	end  
	if club_info[club_id] == nil then
		return nil  
	end 
	return club_info[club_id]
end

function CMD.is_club_name(club_name)
    for i,v in pairs(club_info) do  
    	if v.club_name == club_name then
    		return 1
    	end
    end
    return 0
end

function CMD.del_club_info(club_id)
	club_info[club_id] = nil
end

function CMD.get_club_user(uid,club_id)
	club_id = math.floor(club_id)
	if game_club_user[club_id] == nil then  
		game_club_user[club_id] = {}
        local rs = skynet.call("MG_SAVE", "lua", "get_club",0,31,{club_id=club_id})
        if rs ~= nil then  
	        for i,v in pairs(rs) do  
	        	game_club_user[club_id][v.uid] = v.type
	        end
        else
        	return 0
        end
	end 
	-- print('===game_club_user[club_id]====',club_id,json.encode(game_club_user[club_id]))
	if game_club_user[club_id][uid] == nil then
		return 0  
	end 
	return game_club_user[club_id][uid]
end

function CMD.del_club_user(club_id)
	game_club_user[club_id] = nil
end
 
function CMD.add_club_game(uid,room_id)
	uid = math.floor(uid)
	room_id = math.floor(room_id)
	if club_user_game[uid] == nil then
		club_user_game[uid] = {}
	end
	club_user_game[uid][room_id] = 1
end

function CMD.del_club_game(uid,room_id)
	uid = math.floor(uid)
	room_id = math.floor(room_id)
	if club_user_game[uid] ~= nil and club_user_game[uid][room_id] ~= nil then
		club_user_game[uid][room_id] = nil
	end 
end

function CMD.is_club_game(uid)
	uid = math.floor(uid)
	room_id = math.floor(room_id)
	local rs = 0
	if club_user_game[uid] ~= nil then 
        for i,v in pairs(club_user_game[uid]) do  
        	if v == 1 then
        		rs = 1
        		break
        	end
        end 
	end
	return rs
end

local game_club_room = {}
local game_club_room_user = {}
local game_club_type = {}

function CMD.get_game_club_type(club_id)
	if game_club_type[club_id] == nil then  
		game_club_type[club_id] = {}
        local rs = skynet.call("MG_SAVE", "lua", "get_club",0,10,{club_id=club_id})
        if rs ~= nil then 
            local text = json.decode(rs[1].text)
        	local gameType = math.floor(text.room_info.gameType)
            game_club_type[club_id].hall_name = DATA.get_game_type_name(gameType)

        	text.room_info.club_id = club_id
        	text.room_info.club_name = rs[1].club_name 
        	text.room_info.uid = rs[1].uid 
        	text.room_info.rate = rs[1].rate 
        	text.room_info.club_type = rs[1].club_type 
        	text.room_info.game_type = rs[1].game_type 
            game_club_type[club_id].room_info = text.room_info
        else
        	return nil,nil
        end
	end
    -- print(4444,game_club_type[club_id].hall_name)
	return game_club_type[club_id].hall_name,game_club_type[club_id].room_info
end


function CMD.get_club_room(club_id)
	if game_club_room[club_id] == nil then
		game_club_room[club_id] = {}
	end  
    return game_club_room[club_id]
end 

function CMD.get_club_room_user_info(club_id)
	if game_club_room[club_id] == nil then
		game_club_room[club_id] = {}
	end 
	local rs = {}
	local hall_name,room_info = CMD.get_game_club_type(club_id)
	if hall_name ~= nil then 
        for k,v in pairs(game_club_room[club_id]) do
        	if v ~= nil and v > 0 then
	        	local room_fd = skynet.call(hall_name, "lua", "get_rooms", v)
	        	if room_fd ~= nil then 
		        	local user_info = skynet.call(room_fd, "lua", "get_users") 
		        	table.insert(rs,{room_id=v,user_info=user_info})
		        end
	        end
        end
	end

    return rs,room_info
end 

function CMD.is_club_room(game_id)
	if game_club_room == nil then
		game_club_room = {}
	end
    for ki,kv in pairs(game_club_room) do  
	    for i,v in pairs(kv) do  
	    	if v == game_id then
		        return ki
		    end
	    end 
    end 
    return 0
end 

function CMD.add_club_room(club_id,room_id)
	if game_club_room[club_id] == nil then
		game_club_room[club_id] = {}
	end
    table.insert(game_club_room[club_id],room_id)   
    local num = 0
    for i,v in pairs(game_club_room[club_id]) do  
    	num = num + 1
    end
    skynet.call("MG_SAVE", "lua", "update_club_room_num", club_id,num)
end

function CMD.del_club_room(club_id,room_id) 
	if game_club_room_user[room_id] ~= nil then
		game_club_room_user[room_id] = nil
	end 
    local num = 0
    for i,v in pairs(game_club_room[club_id]) do  
    	num = num + 1
    	if v == room_id then
    		game_club_room[club_id][i] = nil
    	end
    end
    skynet.call("MG_SAVE", "lua", "update_club_room_num", club_id,num)
end 
 
function CMD.get_club_user_score(uid,club_id)
	print('===========get_club_user_score==========',uid,club_id)
	uid = math.floor(uid)
	club_id = math.floor(club_id)
	if club_user_score[club_id] == nil then
		club_user_score[club_id] = {}
	end 
	if club_user_score[club_id][uid] == nil then  
		club_user_score[club_id][uid] = 0
        local rs = skynet.call("MG_SAVE", "lua", "get_club",uid,32,{club_id=club_id}) 
        if rs ~= nil and rs[1]['score'] ~= nil then
        	club_user_score[club_id][uid] = rs[1]['score']
        end
	end  
	return club_user_score[club_id][uid]
end

function CMD.update_club_user_score(uid,club_id,score)
	print('===========update_club_user_score====111======',uid,club_id,score,club_user_score[club_id][uid])
	if club_user_score[club_id] == nil then
		club_user_score[club_id] = {}
	end 
	if club_user_score[club_id][uid] == nil then  
		club_user_score[club_id][uid] = 0
	end 
	club_user_score[club_id][uid] = club_user_score[club_id][uid] + score 
end

 
skynet.start(function() 
	skynet.dispatch("lua", function(_,_, command, ...)
		local f = CMD[command]
		skynet.ret(skynet.pack(f(...)))
	end)
	skynet.register "CLUB"
end)

